﻿{-# OPTIONS_GHC -XDeriveDataTypeable -XTemplateHaskell #-}
module Main where

import Text.XML.Light.Xmlable.TH
import qualified Data.Map as Map
import Text.XML.Light.Xmlable.Types
import Text.XML.Light
import Data.Maybe

data T1 = T1 
newtype TS1 = TS1 Integer
newtype TS2 = TS2 String
data T2 = T2 T1 (Maybe TS1) TS2
data T3 = T3 { f1 :: String, f2 :: String } deriving Show
data T4 = T41 T2 | T42 T3 [T2]
data T5 = T51 T4 NewT3 | T52 { f521 :: T3, f522 :: [T2] }
newtype NewT3 = NewT3 T3
data T6 = T61 | T62 | T63 (T5, T5)
data T7 a b = T71 a | T72 b
type T10 = T9
type T93 = T9
data T9 = T91 | T92 | T93 T93 T10

data T11 = T111 | T112 | T113 | T114

-- f = [$(qRep ''T4), $(qRep ''T5), $(qRep ''T6), $(qRep ''T7)]
$(deriveXmlable False ''T1 "T1s")
$(deriveXmlable False ''TS1 "TS1s")
$(deriveXmlable False ''TS2 "TS2s")
$(deriveXmlable False ''T2 "T2s")
$(deriveXmlable False ''T3 "T3s")
$(deriveXmlable False ''NewT3 "NewT3s")

$(deriveXmlable True ''T4 "T4s")
$(deriveXmlable False ''T5 "T5s")
$(deriveXmlable False ''T9 "T9s")
$(deriveXmlable False ''T11 "T11s")

item1 = Item "one" "This is one"
tstMap = Map.fromList [("one", "This is one"), ("2", "This is two")]
tstMap2 = Map.fromList [("one", tstMap), ("2", tstMap)]

main =  
    putStrLn (ppTopElement $ toElement item1) >> (\(Item a b) -> print ("Item " ++ a ++ ": " ++ b)) ((fromElement $ toElement item1) :: Item String String) 
    >> putStrLn (ppTopElement $ toElement tstMap2)
    >> putStrLn "Xml2hs:" >> print ((fromElement $ toElement tstMap2) :: Map.Map String (Map.Map String String))
    >> putStrLn "Derived T3:" 
    >> print (toElement $ T3 "testT3" "testT2")
    >> putStrLn (ppTopElement $ toElement $ T3 "testT3" "testT2")
    >> putStrLn (ppTopElement $ toElement $ T42 (T3 "testT4" "testT2") [T2 T1 (Just $ TS1 1) $ TS2 "asd"])
    >> putStrLn (ppTopElement $ toElement [T52 (T3 "testT4" "testT2") [T2 T1 Nothing $ TS2 "asdf", T2 T1 (Just $ TS1 212) $ TS2 "asfdaf"], 
            T51 (T41 $ T2 T1 (Just $ TS1 12) $ TS2 "gsd") $ NewT3 $ T3 "str 1" "str 2"])
    >> putStrLn (ppTopElement $ toElement $ T93 (T93 (T93 T92 T91) T92) T91)
    >> putStrLn (ppTopElement $ toElement [T112, T111, T114, T111])
    >> putStrLn "Xml2hs:" >> print ((fromElement $ toElement $ T3 "testT3" "testT2") :: T3)
 